using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Acms.Bll;
using Telerik.Web.UI;

public partial class PostUploadDetails : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get 
        {
            return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Administrator, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.UserGeneratedContentEditor };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            try
            {
                postUploadId = int.Parse(Request.QueryString["Id"]);
            }
            catch
            {
            }

            GridSortExpression tmp = new GridSortExpression();
            tmp.FieldName = gvTotals.Columns[0].SortExpression;
            gvTotals.MasterTableView.SortExpressions.Add(tmp);

            DataBind();
        }
    }


    int postUploadId;
    private Acms.Bll.UserPostUpload _upload;
    protected Acms.Bll.UserPostUpload Upload
    {
        get
        {
            if (_upload == null && postUploadId > 0)
            {
                _upload = Acms.Bll.UserPostUpload.GetById(postUploadId);
            }   
            return _upload;
        }
    }


    public override void DataBind()
    {
        lnkApprove.Visible = false;
        lnkSuspend.Visible = false;
        if (Upload != null)
        {
            Acms.Bll.Role.Roles[] editRoles = new Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.UserGeneratedContentEditor};

            lnkViewUploads.NavigateUrl = string.Format("~/PostUploads.aspx?Id={0}", Upload.PostId.ToString());
            if (User.HasRole(editRoles) && (!Upload.Reviewed || Upload.Suspended))
            {
                lnkApprove.Visible = true;
            }

            if (User.HasRole(editRoles) && !Upload.Suspended)
            {
                lnkSuspend.Visible = true;
            }

            lblTitle.Text = Upload.Title;
            lblUserName.Text = Upload.UserName;
            lblIPAddress.Text = Upload.IPAddress.ToString();
            if (Upload.Comment != null)
            {
                lblComment.Text = Upload.Comment.Replace("\n", "<br/>");
            }
            imgFull.FileId = imgThumb.FileId = Upload.FileId;

            rating.CurrentRating = (int)Math.Round((decimal)Upload.AverageVote * rating.MaxRating, MidpointRounding.AwayFromZero);
            if (Upload.Suspended)
            {
                lblStatus.Text = "Suspended";
            }
            else if (Upload.Reported)
            {
                lblStatus.Text = "Reported";
            }
            else if (Upload.Reviewed)
            {
                lblStatus.Text = "Approved";
            }
            else
            {
                lblStatus.Text = "Not Reviewed";
            }
        }
        base.DataBind();
    }

    protected override object SaveViewState()
    {
        ViewState["PostUploadId"] = postUploadId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if(ViewState["PostUploadId"] is int)
            postUploadId = (int)ViewState["PostUploadId"];
    }

    protected void dsTotals_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UserPostUploadVoteTotal> e)
    {
        List<UserPostUploadVoteTotal> tmp = UserPostUploadVoteTotal.GetAllByUploadId(postUploadId);
        Acms.Bll.UserPostUploadVoteTotal.Sort(tmp, e.SortExpression);
        e.SetData(tmp);
    }
    protected void lnkSuspend_Click(object sender, EventArgs e)
    {
        if (Upload != null)
        {
            Upload.Suspend();
            Upload.Update();
            DataBind();
        }
    }

    protected void lnkApprove_Click(object sender, EventArgs e)
    {
        if (Upload != null)
        {
            Upload.Approve();
            Upload.Update();
            DataBind();
        }
    }
}
